﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WMSWebApi.Interface;
using WMSWebApi.Models;

namespace WMSWebApi.Implement
{
    /// <summary>
    /// 实现物料接口
    /// </summary>
    public class ImpMaterials : IMaterials
    {
        WMSDATAContext _context = new WMSDATAContext();
        public int Add(MaterialsTable item)
        {
            var material = new MaterialsTable
            {
                MaterialCode = item.MaterialCode,
                MaterialName = item.MaterialName,
                MaterialNumber = item.MaterialNumber
            };
            _context.MaterialsTables.Add(material);
            return _context.SaveChanges();
        }
        /// <summary>
        ///删除物料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            var material = _context.MaterialsTables.First(m => m.Id == id);
            _context.MaterialsTables.Remove(material);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 根据名称获取物料
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public MaterialsTable Get(string name)
        {
            return _context.MaterialsTables.First(m => m.MaterialCode == name);
        }
        /// <summary>
        /// 获取所有物料
        /// </summary>
        /// <returns></returns>
        public IEnumerable<MaterialsTable> GetAll()
        {
            return _context.MaterialsTables.ToList<MaterialsTable>();
        }
        /// <summary>
        /// 获取所有物料名称
        /// </summary>
        /// <returns></returns>
        public List<string> GetMaterial()
        {
            List<MaterialsTable> materials = _context.MaterialsTables.ToList<MaterialsTable>();
            List<string> name = new List<string>();
            foreach (var item in materials)
            {
                name.Add(item.MaterialName);
            }
            return name;
        }
        /// <summary>
        /// 修改物料
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(MaterialsTable item)
        {
            var material = new MaterialsTable
            {
                MaterialCode = item.MaterialCode,
                MaterialName = item.MaterialName,
                MaterialNumber = item.MaterialNumber
            };
            _context.MaterialsTables.Update(material);
            return _context.SaveChanges();

        }
    }
}
